package com.zklh.leetcode.interviewTop150;

import com.zklh.leetcode.tree.model.ListNode;

import java.util.Objects;

public class Solution2 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode sumPrev = new ListNode();
        ListNode sumL = sumPrev;
        int carry = 0;
        while (l1 != null || l2 != null || carry != 0) {
            sumL.next = new ListNode();
            sumL = sumL.next;
            sumL.val = (l1 == null ? 0 : l1.val) + (l2 == null ? 0 : l2.val) + carry;
            if (sumL.val >= 10) {
                sumL.val -= 10;
                carry = 1;
            } else {
                carry = 0;
            }
            l1 = l1 != null ? l1.next : null;
            l2 = l2 != null ? l2.next : null;
        }
        return sumPrev.next;
    }
}
